﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Class_KetNoi;

namespace GiaoDien
{
    public partial class uc_PhongBan : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_PhongBan()
        {
            InitializeComponent();
        }
        KetNoi_PhongBan kn = new KetNoi_PhongBan();
        public void Nut()
        {
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            btnHuy.Enabled = true;
        }
        public void LoadDuLieu()
        {
            string sql = "select * from tbl_PhongBan";
            gridControl1.DataSource = kn.taobang(sql);
        }
        private void uc_PhongBan_Load(object sender, EventArgs e)
        {
            kn.connect();
            lbMaPhongBan.Hide();
            txtMaPhongBan.Hide();
            LoadDuLieu();
        }
        int flag;
        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 0;
            Nut();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 1;
            Nut();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            kn.xoa_PhongBan(txtMaPhongBan.Text.Trim());
            LoadDuLieu();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                kn.them_PhongBan(txtTenPhongBan.Text, mskSoDienThoai.Text, txtFax.Text);
            }
            if (flag == 1)
            {
                kn.sua_PhongBan(txtMaPhongBan.Text, txtTenPhongBan.Text, mskSoDienThoai.Text, txtFax.Text);
            }
            LoadDuLieu();
            btnThem.Enabled = true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;

            btnLuu.Enabled = false;
            btnHuy.Enabled = false;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtMaPhongBan.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"MaPB").ToString();
            txtTenPhongBan.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"TenPB").ToString();
            mskSoDienThoai.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"DienThoai").ToString();
            txtFax.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "Fax").ToString();
        }


    }
}
